package com.example.stars_project_android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.stars_project_android.R;
import com.example.stars_project_android.entity.GuestBookEntity;

import java.util.List;

/**
 * 我的留言板适配器
 */

public class MyGuestBookAdapter extends BaseAdapter {
    private Context mContext;
    private int layoutResId;
    private List<GuestBookEntity> myGuestBookList;

    public MyGuestBookAdapter(Context mContext, int layoutResId, List<GuestBookEntity> myGuestBookList) {
        this.mContext = mContext;
        this.layoutResId = layoutResId;
        this.myGuestBookList = myGuestBookList;
    }

    @Override
    public int getCount() {
        if(myGuestBookList != null){
            return myGuestBookList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(myGuestBookList != null){
            return myGuestBookList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            //加载item的布局文件，生成一个View对象，再给convertView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.userImg = convertView.findViewById(R.id.iv_user_img);
            viewHolder.userId = convertView.findViewById(R.id.tv_userId);
            viewHolder.userMessage = convertView.findViewById(R.id.tv_message);
            viewHolder.gridView = convertView.findViewById(R.id.grid);
            viewHolder.messageTime = convertView.findViewById(R.id.tv_message_time);
            viewHolder.CommentsNum = convertView.findViewById(R.id.tv_comments_number);
            viewHolder.likeNum = convertView.findViewById(R.id.tv_like_number);
//            viewHolder.likeBtn = convertView.findViewById(R.id.btn_heart);
            //将ViewHolder对象同convertView关联起来
            convertView.setTag(viewHolder);
        }else {//重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        viewHolder.userImg.setImageResource(myGuestBookList.get(position).getUserImg());
        viewHolder.userId.setText(myGuestBookList.get(position).getUserId());
        viewHolder.userMessage.setText(myGuestBookList.get(position).getUserMessage());
        viewHolder.gridView.setAdapter(new GridGuestAdapter(
                convertView.getContext(),
                R.layout.grid_guest,
                myGuestBookList.get(position).getImg()
        ));
        viewHolder.messageTime.setText(myGuestBookList.get(position).getMessageTime());
        viewHolder.CommentsNum.setText(myGuestBookList.get(position).getCommentsNum());
        viewHolder.likeNum.setText(myGuestBookList.get(position).getLikeNum());
        return convertView;
    }

    private static class ViewHolder{
        ImageView userImg;//用户头像
        TextView userId;//用户id
        TextView userMessage;//用户留言
        GridView gridView;  //发布的图片
        TextView messageTime;//发布时间
        TextView CommentsNum;//留言数
        TextView likeNum;//点赞数
//        Button likeBtn;//点赞按钮

    }
}
